package com.jxust.vhr.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jxust.debate.pojo.Cacode;
import com.jxust.debate.util.CommonResult;
import com.jxust.debate.util.ResultEnum;
import com.jxust.vhr.dao.CacodeDao;
import com.jxust.vhr.service.CacodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CacodeServiceImpl implements CacodeService {


    @Autowired
    private CacodeDao cacodeDao;

    /**
     * 验证认证码是否有效
     * @param cacode 认证码
     * @return 返回成功信息
     */
    @Override
    public CommonResult VerifyCode(Cacode cacode) {
        CommonResult commonResult=new CommonResult();
        cacode=cacodeDao.selectById(cacode.getCacode());
        //判断认证码是否存在
        if(cacode!=null && !StrUtil.hasEmpty(cacode.getCacode())){
            //判断认证码是否已被使用
            if(StrUtil.hasEmpty(cacode.getCaid())){
                commonResult.setResultEnum(ResultEnum.SUCCESS);
            }else {
                commonResult.setCode(ResultEnum.FAILURE.getCode());
                commonResult.setMessage("此认证码已被使用");
            }
        }else {
            commonResult.setCode(ResultEnum.FAILURE.getCode());
            commonResult.setMessage("认证码错误");
        }
        return commonResult;
    }
}
